package org.fh.controller.fhoa;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.fh.controller.base.BaseController;
import org.fh.entity.Page;
import org.fh.entity.PageData;
import org.fh.service.fhoa.CustomerPositionService;
import org.fh.util.DateUtil;
import org.fh.util.Jurisdiction;
import org.fh.util.ObjectExcelView;
import org.fh.util.Tools;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.*;

/**
 * 客户岗位业务
 */
@Controller
@RequestMapping("/customerposition")
public class CustomerPositionController extends BaseController {


    @Autowired
    private CustomerPositionService customerPositionService;

    @Autowired
    private HttpServletRequest request;

    /**
     * 保存
     *
     * @param POSITION_NAME 岗位名称
     * @param POSITION_DESC 岗位描述
     * @return 操作结果
     * @throws Exception 异常
     */
    @RequestMapping(value = "/add")
    @RequiresPermissions("customerposition:add")
    @ResponseBody
    public Object add(
            @RequestParam(value = "POSITION_NAME", required = false) String POSITION_NAME,
            @RequestParam(value = "POSITION_DESC", required = false) String POSITION_DESC
    ) throws Exception {
        Map<String, Object> map = new HashMap<>();
        String errInfo = "success";
        PageData pd = new PageData();

        pd.put("POSITION_NAME", POSITION_NAME);
        pd.put("POSITION_DESC", POSITION_DESC);
        pd.put("USERNAME", Jurisdiction.getUsername());
        pd.put("CREATE_TIME", DateUtil.date2Str(new Date()));
        pd.put("POSITION_ID", this.get32UUID());

        customerPositionService.save(pd);

        map.put("result", errInfo);
        return map;
    }

    /**
     * 删除
     *
     * @return 操作结果
     * @throws Exception 异常
     */
    @RequestMapping(value = "/delete")
    @RequiresPermissions("customerposition:del")
    @ResponseBody
    public Object delete() throws Exception {
        Map<String, String> map = new HashMap<>();
        String errInfo = "success";
        PageData pd = new PageData();
        pd = this.getPageData();
        customerPositionService.delete(pd);
        map.put("result", errInfo);
        return map;
    }

    /**
     * 修改
     *
     * @param POSITION_NAME 岗位名称
     * @param POSITION_DESC 岗位描述
     * @param POSITION_ID   岗位ID
     * @return 操作结果
     * @throws Exception 异常
     */
    @RequestMapping(value = "/edit")
    @RequiresPermissions("customerposition:edit")
    @ResponseBody
    public Object edit(
            @RequestParam(value = "POSITION_NAME", required = false) String POSITION_NAME,
            @RequestParam(value = "POSITION_DESC", required = false) String POSITION_DESC,
            @RequestParam(value = "POSITION_ID", required = false) String POSITION_ID
    ) throws Exception {
        Map<String, Object> map = new HashMap<>();
        String errInfo = "success";
        PageData pd = new PageData();

        pd.put("POSITION_NAME", POSITION_NAME);
        pd.put("POSITION_DESC", POSITION_DESC);
        pd.put("USERNAME", Jurisdiction.getUsername());
        pd.put("CREATE_TIME", DateUtil.date2Str(new Date()));
        pd.put("POSITION_ID", POSITION_ID);

        customerPositionService.edit(pd);

        map.put("result", errInfo);
        return map;
    }

    /**
     * 去修改页面
     *
     * @return 操作结果
     * @throws Exception 异常
     */
    @RequestMapping(value = "/goEdit")
    @ResponseBody
    public Object goEdit() throws Exception {
        Map<String, Object> map = new HashMap<>();
        String errInfo = "success";
        PageData pd = new PageData();
        pd = this.getPageData();
        pd = customerPositionService.findById(pd);
        map.put("pd", pd);
        map.put("result", errInfo);
        return map;
    }

    /**
     * 列表
     *
     * @param page 分页信息
     * @return 操作结果
     * @throws Exception 异常
     */
    @RequestMapping(value = "/list")
//        @RequiresPermissions("customerPosition:list")
    @ResponseBody
    public Object list(Page page) throws Exception {
        Map<String, Object> map = new HashMap<>();
        String errInfo = "success";
        PageData pd = new PageData();
        pd = this.getPageData();
        String KEYWORDS = pd.getString("KEYWORDS");
        if (Tools.notEmpty(KEYWORDS)) {
            pd.put("KEYWORDS", KEYWORDS.trim());
        }
        String USERNAME = Jurisdiction.getUsername();

        page.setPd(pd);
        List<PageData> varList = customerPositionService.list(page);
        map.put("varList", varList);
        map.put("page", page);
        map.put("USERNAME", USERNAME);
        map.put("result", errInfo);
        return map;
    }

    /**
     * 导出到excel
     *
     * @return 操作结果
     * @throws Exception 异常
     */
    @RequestMapping(value = "/excel")
    @RequiresPermissions("toExcel")
    public ModelAndView exportExcel() throws Exception {
        ModelAndView mv = new ModelAndView();
        PageData pd = new PageData();
        pd = this.getPageData();
        Map<String, Object> dataMap = new HashMap<>();
        List<String> titles = new ArrayList<>();
        titles.add("客户岗位 ID");
        titles.add("客户岗位名称");
        titles.add("客户岗位描述");
        titles.add("用户名");
        titles.add("创建时间");
        dataMap.put("titles", titles);
        List<PageData> varOList = customerPositionService.listAll(pd);
        List<PageData> varList = new ArrayList<>();
        for (int i = 0; i < varOList.size(); i++) {
            PageData vpd = new PageData();
            vpd.put("var1", varOList.get(i).getString("POSITION_ID"));
            vpd.put("var2", varOList.get(i).getString("POSITION_NAME"));
            vpd.put("var3", varOList.get(i).getString("POSITION_DESC"));
            vpd.put("var4", varOList.get(i).getString("USERNAME"));
            vpd.put("var5", varOList.get(i).getString("CREATE_TIME"));
            varList.add(vpd);
        }
        dataMap.put("varList", varList);
        ObjectExcelView erv = new ObjectExcelView();
        mv = new ModelAndView(erv, dataMap);
        return mv;
    }
}
